package library.and.xps.com.step.view.base;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import io.realm.Realm;
import library.and.xps.com.step.R;
import library.and.xps.com.step.m.model.User;

public abstract class BaseFragment extends Fragment implements View.OnClickListener,Handler.Callback {
    protected abstract int getRes();

    protected abstract void init(View view);

    protected abstract void setData(View view);

    protected View view = null;
    protected Realm realm;
    protected Handler bkThreadHandler;
    protected Handler mainThreadHandler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(getRes(), container, false);

        try {
            realm = Realm.getDefaultInstance();
        } catch (IllegalStateException ill) {
            ill.printStackTrace();
        }

        HandlerThread handlerThread = new HandlerThread("BackgroundThreadHandler_Fragment");
        handlerThread.start();
        bkThreadHandler = new Handler(handlerThread.getLooper(),this);
        mainThreadHandler = new Handler(Looper.getMainLooper(),this);
        init(view);
        setData(view);
        return view;
    }

    protected User getUser() {
        User user;
        if (realm != null) {
            user = realm.where(User.class).findFirst();
            return user;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    protected <T extends View> T r2v(View view, int resId) {
        return (T) view.findViewById(resId);
    }

@Override
public boolean handleMessage(Message msg) {
    return false;
}
}
